package com.month.security.service.ipml;
import com.month.security.dto.response.LoginUser;
import com.month.security.entity.SysUser;
import com.month.security.service.SysUserService;
import com.month.security.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Set;

/**
 * 用户验证处理类
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {


    @Autowired
    SysUserService sysUserService;

    @Override
    public UserDetails loadUserByUsername(String username){

        SysUser sysUser = sysUserService.selectUserByUserName(username);
        if(StringUtils.isNull(sysUser)){
            throw new UsernameNotFoundException("用户名不存在");
        }

        return createLoginUser(sysUser);
    }



    //对于密码登录
    private UserDetails createLoginUser(SysUser sysUser){
        return new LoginUser(sysUser);
    }
}
